using AppToolKit.Core.AttributeDiscovery;
using AppToolKit.Core.Logging;
using Common.Logging;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace AppToolKit.Core.Context.Spring.AutoConstruction
{
	public class AutoConstructionTypeLocation : IFeatureProcessor
	{
		private static ILog logger_ = LogManager.GetLogger(DefaultLogger.LOGGER_NAME);
		private Dictionary<Type, Type> location_ = new Dictionary<Type, Type>();
		public bool ContainImplement(Type interfaceType)
		{
			return this.location_.ContainsKey(interfaceType);
		}
		public Type GetImplementType(Type interfaceType)
		{
			if (this.location_.ContainsKey(interfaceType))
			{
				return this.location_[interfaceType];
			}
			throw new AutoConstructionException(string.Format("没有找到实现{0}的类", interfaceType));
		}
		public bool IsSupport(Assembly assembly)
		{
			return assembly.IsDefined(typeof(AssemblyNeedDetectAttribute), true);
		}
		public void Process(Type type, FeatureProcessContext ctx)
		{
			if (AutoConstructionTypeLocation.logger_.IsDebugEnabled)
			{
				AutoConstructionTypeLocation.logger_.Debug(string.Format("正在检查{0}", type));
			}
			if (type.IsDefined(typeof(ImplementAttribute), true))
			{
				object[] customAttributes = type.GetCustomAttributes(typeof(ImplementAttribute), true);
				if (customAttributes[0] is BusinessImplAttribute)
				{
					Type interfaceType = ((BusinessImplAttribute)customAttributes[0]).InterfaceType;
					if (this.location_.ContainsKey(interfaceType))
					{
						throw new AutoConstructionException(string.Format("业务接口重复实现{0},{1}", interfaceType, type));
					}
					this.location_.Add(interfaceType, type);
				}
				if (customAttributes[0] is DaoImplAttribute)
				{
					Type interfaceType2 = ((DaoImplAttribute)customAttributes[0]).InterfaceType;
					if (this.location_.ContainsKey(interfaceType2))
					{
						throw new AutoConstructionException(string.Format("Dao接口重复实现{0},{1}", interfaceType2, type));
					}
					this.location_.Add(interfaceType2, type);
				}
				if (customAttributes[0] is FactoryImplAttribute)
				{
					Type entityType = ((FactoryImplAttribute)customAttributes[0]).EntityType;
					if (this.location_.ContainsKey(entityType))
					{
						throw new AutoConstructionException(string.Format("工厂重复实现{0},{1}", entityType, type));
					}
					this.location_.Add(entityType, type);
				}
				if (customAttributes[0] is ServiceImplAttribute)
				{
					Type interfaceType3 = ((ServiceImplAttribute)customAttributes[0]).InterfaceType;
					if (this.location_.ContainsKey(interfaceType3))
					{
						throw new AutoConstructionException(string.Format("服务重复实现{0},{1}", interfaceType3, type));
					}
					this.location_.Add(interfaceType3, type);
				}
				AutoConstructionTypeLocation.logger_.Debug(string.Format("处理的类型{0}", type));
			}
		}
		public void ProcessAssembly(Assembly assembly)
		{
		}
	}
}
